/*
 * main.c
 *
 *  Created on: 2012/09/24
 *      Author: owner
 */

#include <stdio.h>
#include "typedef.h"
#include "Id.h"
#include "lib_func.h"
#include "core.h"
#include "trace.h"
#include "core_internal.h"
#include "core_table.h"
#include "trace_cmd_table.h"
#include "./cg/cg.h"
#include "coredef.h"

//#include "../CCore/trace/trace_cmd/trace_cmd.h"
//static CHAR file[OS_FULLPATH];

#include "core_input.h"
#include "db.h"

static CHAR buf_conf[OS_FULLPATH];
static CHAR buf_ws[OS_FULLPATH];
static CHAR buf[OS_FULLPATH];

int main(int argc, char**arv) {
	S4 nRet;

	PSTRLIST list = NULL;
	sqlite3 *db = NULL;

	/*arg num check*/
	if (argc <= 1) {
		printf("RUN IT LIKE: core -conf [param1] -ws [param2]\n");
		return 0;
	}

	if (argc > 5) {
		printf("ERROR: too many arguments!\n");
		return 0;
	} else if (argc < 5) {
		printf("ERROR: arguments not enough!\n");
		return 0;
	}

	/*-conf*/
	strcpy(buf_conf, arv[1]);
	if (strcmp(buf_conf, "-conf")) {
		printf("ERROR: no '-conf' or '-conf' error!\n");
		return 0;
	}
	strcpy(buf_conf, arv[2]);

	/*-ws*/
	strcpy(buf_ws, arv[3]);
	if (strcmp(buf_ws, "-ws")) {
		printf("ERROR: no '-ws' or '-ws' error!\n");
		return 0;
	}
	strcpy(buf_ws, arv[4]);

	if (buf_ws[strlen(buf_ws) - 1] != '/'
			&& buf_ws[strlen(buf_ws) - 1] != '\\') {
		buf_ws[strlen(buf_ws)] = '/';
	}

	/*analyzeInput*/
	analyzeInput(buf_conf, buf_ws);

	if (stInputInfo.inSrcListHead == NULL ) {
		printf("ERROE: no SRC-input path!\n");
		freeInputInfo();
		return 0;
	}

	if (stInputInfo.inDbPath == NULL ) {
		printf("ERROE: no DB-output path!\n");
		freeInputInfo();
		return 0;
	}

	/*start core*/
	nRet = CCoreStart();
	if (nRet != COEXIT_SUCCESS) {
		printf("sucks!\n");
		return 0;
	}

	/*add core option*/
	/*for test, must be changed*/
	//list = stInputInfo.inSetListHead;
	//while (list != NULL ) {
	//	if (list->str != NULL ) {
	//		if (list->str[0] == '-') {
	//			CCoreAddOptByString(list->str);
	//		}
	//	}
	//	list = list->next;
	//}
	CCoreAddOptByString("-extension_bitfield_type");
	CCoreAddOptByString("-cpp_comment");

	/*add define macro*/
	list = stInputInfo.inDefListHead;
	while (list != NULL ) {
		if (list->str != NULL ) {
			printf("-D %s\n", list->str);
			CCoreAddOpt("-D", list->str);
		}
		list = list->next;
	}

	/*add udef macro*/
	list = stInputInfo.inUDefListHead;
	while (list != NULL ) {
		if (list->str != NULL ) {
			printf("-U %s\n", list->str);
			CCoreAddOpt("-U", list->str);
		}
		list = list->next;
	}

	/*add include files*/
	list = stInputInfo.inIncListHead;
	while (list != NULL ) {
		if (list->str != NULL ) {
			if (COEXIT_FOUND == FileCheckDir(list->str)) {
				printf("-I %s\n", list->str);
				CCoreAddOpt("-I", list->str);
			}
		}
		list = list->next;
	}

	/*open and initialize db*/
	if (FileCheckExists(stInputInfo.inDbPath) == COEXIT_FOUND) {
		printf("WARNING: already existed %s deleted!\n", stInputInfo.inDbPath);
		strcpy(buf, stInputInfo.inDbPath);
		remove(buf);
	}
	sqlite3_open(stInputInfo.inDbPath, &db);
	initCppDB(db);
	initCcDB(db);
	initLocation(db);
	initOption(db);
	initCgDB(db);

	/*cpp and cc,write cpp and cc data*/
	list = stInputInfo.inSrcListHead;
	while (list != NULL ) {
		if (list->str != NULL ) {
			if (COEXIT_FOUND == FileCheckExists(list->str)) {
				if (COEXIT_NFOUND == FileCheckDir(list->str)) {
					strcpy(buf, list->str);
					nRet = CCoreCC(buf, TRUE);

					insertCppTables(db, CTranslationUnit.getMax());
					insertCcTables(db, CTranslationUnit.getMax());

					if (nRet == COEXIT_SUCCESS) {
						nRet = CGProcessing(CTranslationUnit.getMax());
					}
				}
			}
		}
		list = list->next;
	}

	/*link*/
	LinkageSetting();

	/*write cg data*/
	insertLocation(db);
	insertOption(db);
	insertCgTables(db);

	/*free input*/
	freeInputInfo();

	/*close db*/
	sqlite3_close(db);

	/*end core*/
	CCoreTerminate();
	return 1;
}

